package cn.org.mrliu.ssm.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.org.mrliu.ssm.common.web.controller.BaseController;
import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.constant.LogConstant;
import cn.org.mrliu.ssm.constant.RequestUrl;
import cn.org.mrliu.ssm.model.Employee;
import cn.org.mrliu.ssm.service.EmployeeService;
import cn.org.mrliu.ssm.util.Pagination;

@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(EmployeeController.class);
	private static final String DESCRIPTION = "Employee";
	@Resource
	private EmployeeService employeeService;
	
	/**
	 * 跳转到当前模块的主页面
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.INDEX)
	public String index(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"index");
		log.debug(LogConstant.MODEL+model);
		return "employee/index";
	}
	
	/**
	 * 根据ID获取对象数据
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.DETAIL)
	public String detail(@PathVariable("id")Integer id,Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"detail");
		log.debug(LogConstant.ID,id);
		Employee employee = employeeService.getModel(id);
		model.addAttribute("employee", employee);
		log.debug(LogConstant.MODEL+model);
		return "employee/detail";
	}
	
	/**
	 * 跳转到添加页面
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.ADD)
	public String add(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"add");
		log.debug(LogConstant.MODEL+model);
		return "employee/add";
	}
	
	/**
	 * 保存数据
	 * @param employee
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.SAVE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(Employee employee) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"save");
		log.debug("employee:"+employee);
		try {
			employeeService.saveModel(employee);
			log.info(LogConstant.SAVE_SUCCESS,DESCRIPTION); 
			return new ResponseMessage("保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.SAVE_FAILURE,DESCRIPTION); 
			e.printStackTrace();
		}
		return new ResponseMessage(false, "保存失败!");
	}
	
	/**
	 * 跳转到编辑页面之前获取对象数据
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.EDIT)
	public String edit(@PathVariable("id")Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"edit");
		log.debug(LogConstant.ID,id);
		Employee employee = employeeService.getModel(id);
		model.addAttribute("employee", employee);
		log.debug(LogConstant.MODEL+model);
		return "employee/edit";
	}

	/**
	 * 保存数据
	 * @param employee
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.UPDATE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage update(Employee employee) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"update");
		log.debug("employee:"+employee);
		try {
			employeeService.updateModel(employee);
			log.info(LogConstant.UPDATE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.UPDATE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "修改失败!");
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.DELETE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delete(@PathVariable("id")Integer id) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"delete");
		log.debug(LogConstant.ID,id);
		try {
			employeeService.deleteModel(id);
			log.info(LogConstant.DELETE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("删除成功!");
		} catch (Exception e) {
			log.info(LogConstant.DELETE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "删除失败!");
	}
	
	/**
	 * 获取分页列表
	 * @param queryParam 查询参数
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.LIST)
	public String list(QueryParam queryParam, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"list");
		log.debug("queryParam:"+queryParam);
		Pagination pagination = employeeService.getPagination(queryParam);
		model.addAttribute("employeeList", pagination);
		log.debug(LogConstant.MODEL+model);
		return "employee/list";
	}
	
	@RequestMapping("/list1")
	@ResponseBody
	public Map<String, Object> list1(QueryParam queryParam, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"list1");
		log.debug("queryParam:"+queryParam);
		Pagination pagination = employeeService.getPagination(queryParam);
		model.addAttribute("employeeList", pagination);
		Map<String, Object> map = new HashMap<>();
		/**
		 * "draw": parseInt(json.employeeList.pageNum),
            "recordsTotal": parseInt(json.employeeList.total),
            "recordsFiltered": parseInt(json.employeeList.total),
            "data": json.employeeList.list
		 */
		map.put("draw", pagination.getPageNum());
		map.put("recordsTotal", pagination.getTotal());
		map.put("recordsFiltered", pagination.getTotal());
		map.put("data", pagination.getList());
		
		
		String json = JSON.toJSONString(map,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteDateUseDateFormat,
				SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.PrettyFormat
				);
		System.out.println(json);
		
		log.debug(LogConstant.MODEL+model);
		return map;
	}
	
	/**
	 * 批量保存对象
	 * @param employees JSON数组
	 * @return
	 */
	@RequestMapping(value=RequestUrl.BATCH_SAVE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchSave(@RequestBody Employee[] employees) {
		log.debug(LogConstant.EXCUTE_METHOD,"batchSave");
		log.debug("employees:"+employees);
		try {
			employeeService.batchSaveModel(employees);
			log.info(LogConstant.BATCH_SAVE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_SAVE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量保存失败!");
	}
	
	/**
	 * 批量修改
	 * @param employee
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value=RequestUrl.BATCH_UPDATE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchUpdate(Employee employee,String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"batchUpdate");
		log.debug(LogConstant.ID_String+ids);
		log.debug("employee:"+employee);
		try {
			String[] idStrings = handleIdString(ids);
			System.out.println(ids);
			log.debug(LogConstant.IDS+ids);
			employeeService.batchUpdateModel(employee, idStrings);
			log.info(LogConstant.BATCH_UPDATE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_UPDATE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量修改失败!");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value=RequestUrl.BATCH_DELETE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchDelete(String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"batchDelete");
		log.debug(LogConstant.ID_String+ids);
		try {
			String[] idStrings = handleIdString(ids);
			log.debug(LogConstant.IDS+ids);
			employeeService.batchDeleteModel(idStrings);
			log.info(LogConstant.BATCH_DELETE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量删除成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_DELETE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量删除失败!");
	}
	
}
